package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CurePigDAO;
import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeEmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.FeedDAO;
import cn.edu.scau.cmi.hombio.dao.FodderStorageDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTakeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.RoleEmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskEmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskManageDAO;

import cn.edu.scau.cmi.hombio.domain.CurePig;
import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Except;
import cn.edu.scau.cmi.hombio.domain.ExceptionTypeEmployee;
import cn.edu.scau.cmi.hombio.domain.Feed;
import cn.edu.scau.cmi.hombio.domain.FodderStorage;
import cn.edu.scau.cmi.hombio.domain.FodderTake;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineTake;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.RoleEmployee;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskEmployee;
import cn.edu.scau.cmi.hombio.domain.TaskManage;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Employee entities
 * 
 */

@Service("EmployeeService")

@Transactional
public class EmployeeServiceImpl implements EmployeeService {

	/**
	 * DAO injected by Spring that manages CurePig entities
	 * 
	 */
	@Autowired
	private CurePigDAO curePigDAO;

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Except entities
	 * 
	 */
	@Autowired
	private ExceptDAO exceptDAO;

	/**
	 * DAO injected by Spring that manages ExceptionTypeEmployee entities
	 * 
	 */
	@Autowired
	private ExceptionTypeEmployeeDAO exceptionTypeEmployeeDAO;

	/**
	 * DAO injected by Spring that manages Feed entities
	 * 
	 */
	@Autowired
	private FeedDAO feedDAO;

	/**
	 * DAO injected by Spring that manages FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageDAO fodderStorageDAO;

	/**
	 * DAO injected by Spring that manages FodderTake entities
	 * 
	 */
	@Autowired
	private FodderTakeDAO fodderTakeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineTake entities
	 * 
	 */
	@Autowired
	private MedicineTakeDAO medicineTakeDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeDAO roleEmployeeDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskEmployee entities
	 * 
	 */
	@Autowired
	private TaskEmployeeDAO taskEmployeeDAO;

	/**
	 * DAO injected by Spring that manages TaskManage entities
	 * 
	 */
	@Autowired
	private TaskManageDAO taskManageDAO;

	/**
	 * Instantiates a new EmployeeServiceImpl.
	 *
	 */
	public EmployeeServiceImpl() {
	}

	/**
	 * Save an existing Feed entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeFeeds(Integer id, Feed related_feeds) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Feed existingfeeds = feedDAO.findFeedByPrimaryKey(related_feeds.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfeeds != null) {
			existingfeeds.setId(related_feeds.getId());
			existingfeeds.setTime(related_feeds.getTime());
			existingfeeds.setMemo(related_feeds.getMemo());
			related_feeds = existingfeeds;
		}

		related_feeds.setEmployee(employee);
		employee.getFeeds().add(related_feeds);
		related_feeds = feedDAO.store(related_feeds);
		feedDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing TaskManage entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeTaskManages(Integer id, TaskManage related_taskmanages) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		TaskManage existingtaskManages = taskManageDAO.findTaskManageByPrimaryKey(related_taskmanages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskManages != null) {
			existingtaskManages.setId(related_taskmanages.getId());
			existingtaskManages.setRemind(related_taskmanages.getRemind());
			related_taskmanages = existingtaskManages;
		}

		related_taskmanages.setEmployee(employee);
		employee.getTaskManages().add(related_taskmanages);
		related_taskmanages = taskManageDAO.store(related_taskmanages);
		taskManageDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeFodderTakesForEmployeeId(Integer id, FodderTake related_foddertakesforemployeeid) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		FodderTake existingfodderTakesForEmployeeId = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakesforemployeeid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderTakesForEmployeeId != null) {
			existingfodderTakesForEmployeeId.setId(related_foddertakesforemployeeid.getId());
			existingfodderTakesForEmployeeId.setQuantity(related_foddertakesforemployeeid.getQuantity());
			existingfodderTakesForEmployeeId.setTime(related_foddertakesforemployeeid.getTime());
			existingfodderTakesForEmployeeId.setMemo(related_foddertakesforemployeeid.getMemo());
			related_foddertakesforemployeeid = existingfodderTakesForEmployeeId;
		}

		related_foddertakesforemployeeid.setEmployeeByEmployeeId(employee);
		employee.getFodderTakesForEmployeeId().add(related_foddertakesforemployeeid);
		related_foddertakesforemployeeid = fodderTakeDAO.store(related_foddertakesforemployeeid);
		fodderTakeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Load an existing Employee entity
	 * 
	 */
	@Transactional
	public Set<Employee> loadEmployees() {
		return employeeDAO.findAllEmployees();
	}

	/**
	 * Save an existing Except entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeExcepts(Integer id, Except related_excepts) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Except existingexcepts = exceptDAO.findExceptByPrimaryKey(related_excepts.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexcepts != null) {
			existingexcepts.setId(related_excepts.getId());
			existingexcepts.setBarcode(related_excepts.getBarcode());
			existingexcepts.setReportTime(related_excepts.getReportTime());
			existingexcepts.setMemo(related_excepts.getMemo());
			related_excepts = existingexcepts;
		}

		related_excepts.setEmployee(employee);
		employee.getExcepts().add(related_excepts);
		related_excepts = exceptDAO.store(related_excepts);
		exceptDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing CurePig entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeCurePigs(Integer employee_id, Integer related_curepigs_id) {
		CurePig related_curepigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_curepigs.setEmployee(null);
		employee.getCurePigs().remove(related_curepigs);

		curePigDAO.remove(related_curepigs);
		curePigDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing Except entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeExcepts(Integer employee_id, Integer related_excepts_id) {
		Except related_excepts = exceptDAO.findExceptByPrimaryKey(related_excepts_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_excepts.setEmployee(null);
		employee.getExcepts().remove(related_excepts);

		exceptDAO.remove(related_excepts);
		exceptDAO.flush();

		return employee;
	}

	/**
	 * Save an existing TaskEmployee entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeTaskEmployees(Integer id, TaskEmployee related_taskemployees) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		TaskEmployee existingtaskEmployees = taskEmployeeDAO.findTaskEmployeeByPrimaryKey(related_taskemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskEmployees != null) {
			existingtaskEmployees.setId(related_taskemployees.getId());
			related_taskemployees = existingtaskEmployees;
		}

		related_taskemployees.setEmployee(employee);
		employee.getTaskEmployees().add(related_taskemployees);
		related_taskemployees = taskEmployeeDAO.store(related_taskemployees);
		taskEmployeeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing CurePig entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeCurePigs(Integer id, CurePig related_curepigs) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		CurePig existingcurePigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcurePigs != null) {
			existingcurePigs.setId(related_curepigs.getId());
			existingcurePigs.setCureTime(related_curepigs.getCureTime());
			existingcurePigs.setMemo(related_curepigs.getMemo());
			related_curepigs = existingcurePigs;
		}

		related_curepigs.setEmployee(employee);
		employee.getCurePigs().add(related_curepigs);
		related_curepigs = curePigDAO.store(related_curepigs);
		curePigDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeFodderTakesForWarehousekepperId(Integer employee_id, Integer related_foddertakesforwarehousekepperid_id) {
		FodderTake related_foddertakesforwarehousekepperid = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakesforwarehousekepperid_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_foddertakesforwarehousekepperid.setEmployeeByEmployeeId(null);
		employee.getFodderTakesForEmployeeId().remove(related_foddertakesforwarehousekepperid);

		fodderTakeDAO.remove(related_foddertakesforwarehousekepperid);
		fodderTakeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeExceptionTypeEmployees(Integer employee_id, Integer related_exceptiontypeemployees_id) {
		ExceptionTypeEmployee related_exceptiontypeemployees = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(related_exceptiontypeemployees_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_exceptiontypeemployees.setEmployee(null);
		employee.getExceptionTypeEmployees().remove(related_exceptiontypeemployees);

		exceptionTypeEmployeeDAO.remove(related_exceptiontypeemployees);
		exceptionTypeEmployeeDAO.flush();

		return employee;
	}

	/**
	 */
	@Transactional
	public Employee findEmployeeByPrimaryKey(Integer id) {
		return employeeDAO.findEmployeeByPrimaryKey(id);
	}

	/**
	 * Save an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeMedicineStorages(Integer id, MedicineStorage related_medicinestorages) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		MedicineStorage existingmedicineStorages = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineStorages != null) {
			existingmedicineStorages.setId(related_medicinestorages.getId());
			existingmedicineStorages.setTime(related_medicinestorages.getTime());
			related_medicinestorages = existingmedicineStorages;
		}

		related_medicinestorages.setEmployee(employee);
		employee.getMedicineStorages().add(related_medicinestorages);
		related_medicinestorages = medicineStorageDAO.store(related_medicinestorages);
		medicineStorageDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing Prevention entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeePreventions(Integer employee_id, Integer related_preventions_id) {
		Prevention related_preventions = preventionDAO.findPreventionByPrimaryKey(related_preventions_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_preventions.setEmployee(null);
		employee.getPreventions().remove(related_preventions);

		preventionDAO.remove(related_preventions);
		preventionDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeMedicineTakesForPharmacistId(Integer employee_id, Integer related_medicinetakesforpharmacistid_id) {
		MedicineTake related_medicinetakesforpharmacistid = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakesforpharmacistid_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_medicinetakesforpharmacistid.setEmployeeByPharmacistId(null);
		employee.getMedicineTakesForPharmacistId().remove(related_medicinetakesforpharmacistid);

		medicineTakeDAO.remove(related_medicinetakesforpharmacistid);
		medicineTakeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeMedicineTakesForTakerId(Integer id, MedicineTake related_medicinetakesfortakerid) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		MedicineTake existingmedicineTakesForTakerId = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakesfortakerid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineTakesForTakerId != null) {
			existingmedicineTakesForTakerId.setId(related_medicinetakesfortakerid.getId());
			existingmedicineTakesForTakerId.setTime(related_medicinetakesfortakerid.getTime());
			existingmedicineTakesForTakerId.setMemo(related_medicinetakesfortakerid.getMemo());
			related_medicinetakesfortakerid = existingmedicineTakesForTakerId;
		}

		related_medicinetakesfortakerid.setEmployeeByPharmacistId(employee);
		employee.getMedicineTakesForPharmacistId().add(related_medicinetakesfortakerid);
		related_medicinetakesfortakerid = medicineTakeDAO.store(related_medicinetakesfortakerid);
		medicineTakeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing ExceptionTypeEmployee entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeExceptionTypeEmployees(Integer id, ExceptionTypeEmployee related_exceptiontypeemployees) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		ExceptionTypeEmployee existingexceptionTypeEmployees = exceptionTypeEmployeeDAO.findExceptionTypeEmployeeByPrimaryKey(related_exceptiontypeemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexceptionTypeEmployees != null) {
			existingexceptionTypeEmployees.setId(related_exceptiontypeemployees.getId());
			existingexceptionTypeEmployees.setIsExceptionManager(related_exceptiontypeemployees.getIsExceptionManager());
			related_exceptiontypeemployees = existingexceptionTypeEmployees;
		}

		related_exceptiontypeemployees.setEmployee(employee);
		employee.getExceptionTypeEmployees().add(related_exceptiontypeemployees);
		related_exceptiontypeemployees = exceptionTypeEmployeeDAO.store(related_exceptiontypeemployees);
		exceptionTypeEmployeeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public void saveEmployee(Employee employee) {
		Employee existingEmployee = employeeDAO.findEmployeeByPrimaryKey(employee.getId());

		if (existingEmployee != null) {
			if (existingEmployee != employee) {
				existingEmployee.setId(employee.getId());
				existingEmployee.setNumber(employee.getNumber());
				existingEmployee.setName(employee.getName());
				existingEmployee.setPassword(employee.getPassword());
				existingEmployee.setLogOn(employee.getLogOn());
				existingEmployee.setRfid(employee.getRfid());
				existingEmployee.setSex(employee.getSex());
				existingEmployee.setBirthday(employee.getBirthday());
				existingEmployee.setState(employee.getState());
				existingEmployee.setMemo(employee.getMemo());
			}
			employee = employeeDAO.store(existingEmployee);
		} else {
			employee = employeeDAO.store(employee);
		}
		employeeDAO.flush();
	}

	/**
	 * Delete an existing Sale entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeSales(Integer employee_id, Integer related_sales_id) {
		Sale related_sales = saleDAO.findSaleByPrimaryKey(related_sales_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_sales.setEmployee(null);
		employee.getSales().remove(related_sales);

		saleDAO.remove(related_sales);
		saleDAO.flush();

		return employee;
	}

	/**
	 * Save an existing Inspection entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeInspections(Integer id, Inspection related_inspections) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Inspection existinginspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspections != null) {
			existinginspections.setId(related_inspections.getId());
			existinginspections.setTime(related_inspections.getTime());
			existinginspections.setMemo(related_inspections.getMemo());
			related_inspections = existinginspections;
		}

		related_inspections.setEmployee(employee);
		employee.getInspections().add(related_inspections);
		related_inspections = inspectionDAO.store(related_inspections);
		inspectionDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing TaskEmployee entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeTaskEmployees(Integer employee_id, Integer related_taskemployees_id) {
		TaskEmployee related_taskemployees = taskEmployeeDAO.findTaskEmployeeByPrimaryKey(related_taskemployees_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_taskemployees.setEmployee(null);
		employee.getTaskEmployees().remove(related_taskemployees);

		taskEmployeeDAO.remove(related_taskemployees);
		taskEmployeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing TaskManage entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeTaskManages(Integer employee_id, Integer related_taskmanages_id) {
		TaskManage related_taskmanages = taskManageDAO.findTaskManageByPrimaryKey(related_taskmanages_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_taskmanages.setEmployee(null);
		employee.getTaskManages().remove(related_taskmanages);

		taskManageDAO.remove(related_taskmanages);
		taskManageDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeTasks(Integer employee_id, Integer related_tasks_id) {
		Task related_tasks = taskDAO.findTaskByPrimaryKey(related_tasks_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_tasks.setEmployee(null);
		employee.getTasks().remove(related_tasks);

		taskDAO.remove(related_tasks);
		taskDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeRoleEmployees(Integer employee_id, Integer related_roleemployees_id) {
		RoleEmployee related_roleemployees = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(related_roleemployees_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_roleemployees.setEmployee(null);
		employee.getRoleEmployees().remove(related_roleemployees);

		roleEmployeeDAO.remove(related_roleemployees);
		roleEmployeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing Feed entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeFeeds(Integer employee_id, Integer related_feeds_id) {
		Feed related_feeds = feedDAO.findFeedByPrimaryKey(related_feeds_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_feeds.setEmployee(null);
		employee.getFeeds().remove(related_feeds);

		feedDAO.remove(related_feeds);
		feedDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeFodderStorages(Integer employee_id, Integer related_fodderstorages_id) {
		FodderStorage related_fodderstorages = fodderStorageDAO.findFodderStorageByPrimaryKey(related_fodderstorages_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_fodderstorages.setEmployee(null);
		employee.getFodderStorages().remove(related_fodderstorages);

		fodderStorageDAO.remove(related_fodderstorages);
		fodderStorageDAO.flush();

		return employee;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeTasks(Integer id, Task related_tasks) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Task existingtasks = taskDAO.findTaskByPrimaryKey(related_tasks.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtasks != null) {
			existingtasks.setId(related_tasks.getId());
			existingtasks.setTaskNumber(related_tasks.getTaskNumber());
			existingtasks.setCreateTime(related_tasks.getCreateTime());
			existingtasks.setStartTime(related_tasks.getStartTime());
			existingtasks.setEndTime(related_tasks.getEndTime());
			existingtasks.setMemo(related_tasks.getMemo());
			related_tasks = existingtasks;
		}

		related_tasks.setEmployee(employee);
		employee.getTasks().add(related_tasks);
		related_tasks = taskDAO.store(related_tasks);
		taskDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing Prevention entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeePreventions(Integer id, Prevention related_preventions) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Prevention existingpreventions = preventionDAO.findPreventionByPrimaryKey(related_preventions.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpreventions != null) {
			existingpreventions.setId(related_preventions.getId());
			existingpreventions.setTime(related_preventions.getTime());
			existingpreventions.setMemo(related_preventions.getMemo());
			related_preventions = existingpreventions;
		}

		related_preventions.setEmployee(employee);
		employee.getPreventions().add(related_preventions);
		related_preventions = preventionDAO.store(related_preventions);
		preventionDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Return a count of all Employee entity
	 * 
	 */
	@Transactional
	public Integer countEmployees() {
		return ((Long) employeeDAO.createQuerySingleResult("select count(o) from Employee o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeFodderStorages(Integer id, FodderStorage related_fodderstorages) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		FodderStorage existingfodderStorages = fodderStorageDAO.findFodderStorageByPrimaryKey(related_fodderstorages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderStorages != null) {
			existingfodderStorages.setId(related_fodderstorages.getId());
			existingfodderStorages.setQuantity(related_fodderstorages.getQuantity());
			existingfodderStorages.setTime(related_fodderstorages.getTime());
			related_fodderstorages = existingfodderStorages;
		}

		related_fodderstorages.setEmployee(employee);
		employee.getFodderStorages().add(related_fodderstorages);
		related_fodderstorages = fodderStorageDAO.store(related_fodderstorages);
		fodderStorageDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Return all Employee entity
	 * 
	 */
	@Transactional
	public List<Employee> findAllEmployees(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Employee>(employeeDAO.findAllEmployees(startResult, maxRows));
	}

	/**
	 * Delete an existing Inspection entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeInspections(Integer employee_id, Integer related_inspections_id) {
		Inspection related_inspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_inspections.setEmployee(null);
		employee.getInspections().remove(related_inspections);

		inspectionDAO.remove(related_inspections);
		inspectionDAO.flush();

		return employee;
	}

	/**
	 * Save an existing Sale entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeSales(Integer id, Sale related_sales) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		Sale existingsales = saleDAO.findSaleByPrimaryKey(related_sales.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsales != null) {
			existingsales.setId(related_sales.getId());
			existingsales.setSaleType(related_sales.getSaleType());
			existingsales.setMemo(related_sales.getMemo());
			related_sales = existingsales;
		}

		related_sales.setEmployee(employee);
		employee.getSales().add(related_sales);
		related_sales = saleDAO.store(related_sales);
		saleDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Save an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeRoleEmployees(Integer id, RoleEmployee related_roleemployees) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		RoleEmployee existingroleEmployees = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(related_roleemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingroleEmployees != null) {
			existingroleEmployees.setId(related_roleemployees.getId());
			existingroleEmployees.setMemo(related_roleemployees.getMemo());
			related_roleemployees = existingroleEmployees;
		}

		related_roleemployees.setEmployee(employee);
		employee.getRoleEmployees().add(related_roleemployees);
		related_roleemployees = roleEmployeeDAO.store(related_roleemployees);
		roleEmployeeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeMedicineStorages(Integer employee_id, Integer related_medicinestorages_id) {
		MedicineStorage related_medicinestorages = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorages_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_medicinestorages.setEmployee(null);
		employee.getMedicineStorages().remove(related_medicinestorages);

		medicineStorageDAO.remove(related_medicinestorages);
		medicineStorageDAO.flush();

		return employee;
	}

	/**
	 * Save an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeMedicineTakesForPharmacistId(Integer id, MedicineTake related_medicinetakesforpharmacistid) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		MedicineTake existingmedicineTakesForPharmacistId = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakesforpharmacistid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineTakesForPharmacistId != null) {
			existingmedicineTakesForPharmacistId.setId(related_medicinetakesforpharmacistid.getId());
			existingmedicineTakesForPharmacistId.setTime(related_medicinetakesforpharmacistid.getTime());
			existingmedicineTakesForPharmacistId.setMemo(related_medicinetakesforpharmacistid.getMemo());
			related_medicinetakesforpharmacistid = existingmedicineTakesForPharmacistId;
		}

		related_medicinetakesforpharmacistid.setEmployeeByPharmacistId(employee);
		employee.getMedicineTakesForPharmacistId().add(related_medicinetakesforpharmacistid);
		related_medicinetakesforpharmacistid = medicineTakeDAO.store(related_medicinetakesforpharmacistid);
		medicineTakeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeFodderTakesForEmployeeId(Integer employee_id, Integer related_foddertakesforemployeeid_id) {
		FodderTake related_foddertakesforemployeeid = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakesforemployeeid_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_foddertakesforemployeeid.setEmployeeByEmployeeId(null);
		employee.getFodderTakesForEmployeeId().remove(related_foddertakesforemployeeid);

		fodderTakeDAO.remove(related_foddertakesforemployeeid);
		fodderTakeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public void deleteEmployee(Employee employee) {
		employeeDAO.remove(employee);
		employeeDAO.flush();
	}

	/**
	 * Save an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Employee saveEmployeeFodderTakesForWarehousekepperId(Integer id, FodderTake related_foddertakesforwarehousekepperid) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(id, -1, -1);
		FodderTake existingfodderTakesForWarehousekepperId = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakesforwarehousekepperid.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderTakesForWarehousekepperId != null) {
			existingfodderTakesForWarehousekepperId.setId(related_foddertakesforwarehousekepperid.getId());
			existingfodderTakesForWarehousekepperId.setQuantity(related_foddertakesforwarehousekepperid.getQuantity());
			existingfodderTakesForWarehousekepperId.setTime(related_foddertakesforwarehousekepperid.getTime());
			existingfodderTakesForWarehousekepperId.setMemo(related_foddertakesforwarehousekepperid.getMemo());
			related_foddertakesforwarehousekepperid = existingfodderTakesForWarehousekepperId;
		}

		related_foddertakesforwarehousekepperid.setEmployeeByEmployeeId(employee);
		employee.getFodderTakesForEmployeeId().add(related_foddertakesforwarehousekepperid);
		related_foddertakesforwarehousekepperid = fodderTakeDAO.store(related_foddertakesforwarehousekepperid);
		fodderTakeDAO.flush();

		employee = employeeDAO.store(employee);
		employeeDAO.flush();

		return employee;
	}

	/**
	 * Delete an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Employee deleteEmployeeMedicineTakesForTakerId(Integer employee_id, Integer related_medicinetakesfortakerid_id) {
		MedicineTake related_medicinetakesfortakerid = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakesfortakerid_id, -1, -1);

		Employee employee = employeeDAO.findEmployeeByPrimaryKey(employee_id, -1, -1);

		related_medicinetakesfortakerid.setEmployeeByPharmacistId(null);
		employee.getMedicineTakesForPharmacistId().remove(related_medicinetakesfortakerid);

		medicineTakeDAO.remove(related_medicinetakesfortakerid);
		medicineTakeDAO.flush();

		return employee;
	}
}
